package com.xique.door.controller;

import com.xique.common.core.constant.AdvertFileType;
import com.xique.common.core.constant.OssFilePrefixConstants;
import com.xique.common.core.domain.R;
import com.xique.common.core.enums.StatusCode;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.Information;
import com.xique.door.bean.domain.OssfileUrl;
import com.xique.door.service.IInformationService;
import com.xique.door.service.IOssfileUrlService;
import com.xique.door.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/** 资讯（富文本） */
@Log4j2
@RestController
@RequestMapping("/information")
public class InformationContorller {

  @Autowired private IInformationService iInformationService;

  @Autowired private AliossUtil aliossUtil;

  @Autowired private IOssfileUrlService ossfileUrlService;

  @ResponseBody
  @PostMapping("/uploadOperateImage")
  @Log(title = "oss运营图片", businessType = BusinessType.IMPORT)
  @ApiOperation("oss运营图片")
  public AjaxResult uploadMineImage(
      @RequestParam("file") MultipartFile file,
      @RequestParam(required = false, defaultValue = OssFilePrefixConstants.OPERATE_IMGAGE)
          Integer fileType) {
    String prefix = OssFilePrefixConstants.OPERATE_IMGAGE;
    if (fileType.equals(AdvertFileType.VIDEO)) {
      prefix = OssFilePrefixConstants.OPERATE_VIDEO;
    }
    if (file != null) {
      Map<String, Object> resultMap = aliossUtil.uploadFile(file, prefix);
      if (resultMap.get("code").equals(StatusCode.ERROR.getCode())) {
        return AjaxResult.error("error", "文件上传失败！！！");
      }
      AjaxResult result = AjaxResult.success("success", "文件上传成功");
      result.put("url", resultMap.get("url"));
      result.put("objectName", resultMap.get("objectName"));

      return result;
    } else {
      return AjaxResult.error("error", "文件上传失败！！！");
    }
  }

  @ResponseBody
  @PostMapping("/uploadVideo")
  @Log(title = "oss运营视频", businessType = BusinessType.IMPORT)
  @ApiOperation("oss运营视频")
  public AjaxResult uploadMineVideo(
      @RequestParam("file") MultipartFile file,
      Integer fileType,
      String title,
      String model,
      String type)
      throws Exception {
    if (file != null && fileType != null && title != null) {
      OssfileUrl oss = new OssfileUrl();
      oss.setFileType(fileType);
      oss.setTitle(title);
      oss.setModel(model);
      oss.setType(type);
      oss.setCreateBy(SecurityUtils.getUserId());
      oss.setUpdateBy(SecurityUtils.getUserId());
      oss.setFilesize(FileUtil.FormetFileSize(file.getSize()));
      // 上传的文件
      ossfileUrlService.uploadMineVideo(file, oss);
      // 休眠2秒避免请求结束，文件取不到
      Thread.sleep(2000);
      System.out.println("文件上传中，请稍后");
      return AjaxResult.success("文件上传中，请稍后");
    } else {
      return AjaxResult.error("error", "文件上传失败！！！");
    }
  }

  /** 资讯列表 */
  @PreAuthorize(hasPermi = "door:information:list")
  @GetMapping("/getInformationList")
  public PageResult getInformationList(
      Information information,
      String startTime,
      String endTime,
      String userName,
      Integer pageNum,
      Integer pageSize) {
    return iInformationService.getInformationList(
        information, startTime, endTime, userName, pageNum, pageSize);
  }
  /** 添加资讯 */
  @PreAuthorize(hasPermi = "door:information:add")
  @PostMapping("/addInformation")
  public R addInformation(@RequestBody Information information) {
    return iInformationService.insertInformation(information);
  }

  /** 修改资讯 */
  @PreAuthorize(hasPermi = "door:information:edit")
  @PutMapping("/updateInformation")
  public R updateInformation(@RequestBody Information information) {
    return iInformationService.updateInformation(information);
  }

  /** 资讯详情 */
  @PreAuthorize(hasPermi = "door:information:edit")
  @GetMapping("/getInformation")
  public R getInformation(Long id) {
    return iInformationService.getInformation(id);
  }

  /** 资讯删除 */
  @PreAuthorize(hasPermi = "door:information:delete")
  @GetMapping("/deleteInformation")
  public R deleteInformation(Long id) {
    return iInformationService.deleteInformation(id);
  }
}
